#include "BlockQueue.hpp"
#include <pthread.h>
#include <ctime>
#include <sys/types.h>
#include <unistd.h>
#include "Task.hpp"

void *consumer(void *args)
{
    BlockQueue<Task>* bq = static_cast<BlockQueue<Task> *>(args);
    while(true)
    {
        //1.消费数据
        Task t;
        bq->Pop(&t);
        //2.对数据进行处理
        // t.Run();
        t(); //仿函数
        cout << "consumer data: " << t.PrintResult() << endl;
    }
    return nullptr;
}

void *productor(void *args)
{
    BlockQueue<Task>* bq = static_cast<BlockQueue<Task> *>(args);
    while(true)
    {
        //1.有数据
        int data1 = rand() % 10; //[0, 9]
        int data2 = rand() % 10; //[0, 9]
        char oper = opers[rand() % opers.size()];
        Task t(data1, data2, oper); //构建任务
        //2.进行生产
        bq->Push(t);
        //for debug
        cout << "productor data : " << t.PrintTask() << endl;
        sleep(1);
    }
    return nullptr;
}

int main()
{
    srand((uint16_t)time(nullptr) ^ getpid() ^ pthread_self()); //形成更加随机的数
    BlockQueue<Task> *bq = new BlockQueue<Task>();
    pthread_t c, p;
    pthread_create(&c, nullptr, consumer, bq);
    pthread_create(&c, nullptr, productor, bq);

    pthread_join(c, nullptr);
    pthread_join(p, nullptr);
    return 0;
}